package com.chinasolf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chinasoft.enums.DictEnum;
import com.chinasoft.model.hosp.Hospital;
import com.chinasoft.vo.hosp.HospitalQueryVo;
import com.chinasolf.client.cmn.DictFeignClient;
import com.chinasolf.repository.HospitalRepository;
import com.chinasolf.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void saveHosp(Map<String, Object> map) {
        String jsonString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        Hospital hospital1Exist = hospitalRepository.findByHoscode(hospital.getHoscode());

        if (hospital1Exist != null){
            hospital.setId(hospital1Exist.getId());
            hospitalRepository.save(hospital);
            hospital.setCreateTime(hospital1Exist.getCreateTime());
            hospital.setUpdateTime(new Date());
        }else {
            //如果没有添加，进行添加操作
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }



    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectPage(Integer page,
                                     Integer limit,
                                     HospitalQueryVo hospitalQueryVo) {
        //设置排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //设置分页参数
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //封装条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        Hospital hospital = new Hospital();
        //hospitalQueryVo -- hospital
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example = Example.of(hospital,matcher);
        //调用方法
        Page<Hospital> pageModel = hospitalRepository.findAll(example, pageable);

        //从查询出来pageModel对象获取list集合，
        // 把list集合遍历得到每个hosp对象，根据编号获取名称，进行封装
        pageModel.getContent().stream().forEach(item -> {
            this.packageHosp(item);
        });
        return pageModel;
    }

    @Override
    public Map<String, Object> showHosp(String id) {
        Hospital hospital = this.packageHosp(hospitalRepository.findById(id).get());
        Map<String,Object> result = new HashMap<>();
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        return result;
    }

    @Override
    public List<Hospital> selectHospList(HospitalQueryVo hospitalQueryVo) {
        //设置排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example = Example.of(hospital);
        List<Hospital> hospitalList = hospitalRepository.findAll(example, sort);
        //编号的名称获取封装
        hospitalList.stream().forEach(item->{
            this.packageHosp(item);
        });
        return hospitalList;
    }

    @Override
    public List<Hospital> getHospByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> findHospDetail(String hoscode) {
        Map<String,Object>  result = new HashMap<>();
        Hospital hospital = this.packageHosp(this.getByHoscode(hoscode));
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        return result;
    }


    private Hospital packageHosp(Hospital hosp) {
        //从hosp获取编号
        String hostype = hosp.getHostype();
        String provinceCode = hosp.getProvinceCode();
        String cityCode = hosp.getCityCode();
        String districtCode = hosp.getDistrictCode();
        //远程调用数据编号获取名称
        String provinceString = dictFeignClient.getName(provinceCode);
        String cityString = dictFeignClient.getName(cityCode);
        String districtString = dictFeignClient.getName(districtCode);

        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hostype);
        //进行封装
        hosp.getParam().put("hostypeString",hostypeString);
        hosp.getParam().put("fullAddress", provinceString + cityString + districtString + hosp.getAddress());
        //返回
        return hosp;

    }
}
